// pages/campus-abbr/campus-abbr.js'
let searchDisabled = false
let ifSearch = false
let app = getApp()
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[{
      title:'二手闲置',
      iconClass:'icon-ershouchepinggu',
      url:'',
      activeClass:1
    },{
      title:'校园互助',
      iconClass:'icon-woshou-xianxing',
      url:'',
      activeClass:2
    },
  {
    title:'全部',
      iconClass:'icon-all',
      url:'',
      activeClass:0
  },{
    title:'游玩扩列',
      iconClass:'icon-dangdiwanle',
      url:'',
      activeClass:3
  },{
    title:'兼职实习',
      iconClass:'icon-jianzhi',
      url:'',
      activeClass:4
  }],
    showSidebar:true,
    activeClass: 0,
    activeSubClass: 0,
    itemList: [],
    keyword: '',
    loading: true,
    pageNo:0,
    pageSize:10,
    complete:false,
    showModelWin:false,
    reportReason:'',
    reportId:'',
    showSelectWinFlag:false,
    array: ['美国', '中国', '巴西', '日本'],
    popup:false,
    userInfo:{my:"?"}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type=options.type
    app.setCallBack('loginSuccess',this.loginSuccess,{page:this,type:type})
    console.log(options)
  },

  loginSuccess(that,type){
    let userInfo = app.globalData.userInfo
    console.log(userInfo)
    if(!(userInfo.email || (userInfo.img && userInfo.img.length > 0))){
      wx.showModal({
        title: '提示',
        content: '您好未注册，是否注册页面先进行注册？',
        success: function (res) {
          if (res.confirm) {
            console.log('确定')
            wx.navigateTo({
              url: '/pages/profile-edit/profile-edit',
            })
          } else {
            console.log('取消')
          }
        }
      })
    }
    that._loadItemList(0)
    if(type && type == 'campusComment'){
      wx.navigateTo({
        url: '../../pages/campus-comment/campus-comment?type=0&itemId='+options.itemId
      })
    }
    that.setData({userInfo:userInfo})
  },

  showSelectWin(){
    this.setData({popup:true})
  },
  
  reportInvitation(e){
    let that = this
    console.log(e.currentTarget.dataset.itemId)
    console.log("举报举报举报")
    //this.setData({showModelWin:true})
    wx.showModal({
      title: '举报',
     content: '确定举报改帖子吗？',
     success: function (res) {
       if (res.confirm) {//这里是点击了确定以后
         console.log('用户点击确定')
         that.setData({showModelWin:true,reportId:e.currentTarget.dataset.itemId})
         /*let _url = req.actions.ym+ "invitation/reportInvitation",params={invitation_id:e.currentTarget.dataset.itemId,customer_id:app.globalData.userInfo.id}
         console.log(params)
         utils.myResquest(params,_url,function(res){
            console.log("_loadItemList===>"+JSON.stringify(res))
        })*/
       } else {//这里是点击了取消以后
         console.log('用户点击取消')
       }
     }
    })
  },
  bindTextAreaBlur: function(e) {
    console.log(e.detail.value)
    this.setData({reportReason:e.detail.value})
  },
  modalCandel(){
    that.setData({showModelWin:false,reportReason:'',reportId:''})
  },
  modalConfirm(){
    let that = this
    if(that.data.reportReason.trim() == '') {
      wx.showToast({
        title: '请填写举报原因',
        icon: 'none'
      })
      return
    } 
    let _url = req.actions.ym+ "invitation/reportInvitation",params={invitation_id:this.data.reportId,customer_id:app.globalData.userInfo.id,report_reason:this.data.reportReason}
    console.log(params)
    utils.myResquest(params,_url,function(res){
       console.log("_loadItemList===>"+JSON.stringify(res))
       that.setData({showModelWin:false,reportReason:'',reportId:''})
   })
  },
  
  _loadItemList() {
    console.log("_loadItemList_loadItemList_loadItemList_loadItemList")
    this.setData({
      loading: true
    }, ()=>{
      let data = {
        "pageNo":this.data.pageNo,
        "pageSize":this.data.pageSize,
        "type":""
      },
      that = this
      if(ifSearch) {
        data.keyword = this.data.keyword
      } else {
        this.data.activeClass == '0' ? data.type = "1,2,3,4" : data.type = this.data.activeClass
      }
      data.schoolId = app.globalData.userInfo.schoolId
      console.log("data:"+JSON.stringify(data))
      let _url = req.actions.ym+ "invitation/pageList"
      utils.myResquest(data,_url,function(res){
        console.log("_loadItemList===>"+JSON.stringify(res))
        let rows = res.data.result.records
        if(rows && rows.length < 20){
          that.setData({complete:true})
        }
        that.setData({
          itemList: that.data.itemList ? that.data.itemList.concat(rows) : rows,
          loading:false
        })
        if(!rows || rows.length <= 0){
          that.setData({
            itemList: [{"id":113,"avatar_url":"https://www.zento.fun/xcx/zento/zjz/20220224/1645680141029.jpg","nick_name":"ABC","qq":"","customer_id":"707","img":null,"report_num":0,"title":"课程咨询","content":"求问西方古典学基础硬不硬，貌似爆满","type":"2","sub_type":"4","wechat":"","if_anonymous":"0","like_num":0,"phone":"13213362809","watch_num":67,"comment_num":11,"create_time":"2022-02-24 13:29:19","update_time":"2022-02-24 13:29:19","if_complete":"1","school_id":1,"over_status":"0","school_name":null,"if_top":"0","del_status":"0","my_avatar_url":"https://www.zento.fun/xcx/zento/zjz/20220224/1645680141029.jpg"}],
            loading: false,
          })
        }
      })
      wx.stopPullDownRefresh()
    })
  },
  onClickSearch() {
    let keyword = this.data.keyword
    if(keyword.trim() == '') {
      wx.showToast({
        title: '搜索关键字不能为空',
        icon: 'none'
      })
      return
    } 
    ifSearch = true
    this._loadItemList(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    this.getTabBar().init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    searchDisabled = false
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({pageNo:0,itemList:[]})
    this._loadItemList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.complete) {
      wx.showToast({
        title: '已全部加载~',
        icon: 'none'
      })
      return
    }
    //this._loadItemList(this.data.itemList.length)
    this.setData({pageNo:this.data.pageNo+1})
    this._loadItemList()
  },

  parentOperation(e){
    console.log(e)
    let itemList = this.data.itemList
    if(e.detail.type == 'deleteInvitation'){
      for(var i=0;i<itemList.length;i++){
        if(itemList[i].id == e.detail.id){
          itemList[i].hidden = true
          this.setData({itemList:itemList})
          break;
        }
      }
    }else{
      for(var i=0;i<itemList.length;i++){
        if(itemList[i].id == e.detail.id){
          itemList[i].over = true
          this.setData({itemList:itemList})
          break;
        }
      }
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '这个很有趣，一起看看吧',
      path: `/pages/campus/campus`,
    }
  },
  onChange(event) {
    console.log(event.currentTarget.dataset.id)
    ifSearch = false
    if(this.data.activeClass == event.currentTarget.dataset.id)
      return
    this.setData({
      activeClass:event.currentTarget.dataset.id,
      activeSubClass: 0
    }, ()=>{
      this._loadItemList(0)
    })
  },
  onChangeSub(event) {
    ifSearch = false
    if(this.data.activeSubClass == event.detail)
      return
    this.setData({
      activeSubClass:event.detail
    }, ()=>{
      this._loadItemList()
    })
  },
  async onClickPublish() {
    let name = app.globalData.userInfo.name,userInfo = app.globalData.userInfo
    if (!searchDisabled) {
      searchDisabled = true
      if (!name) {
        wx.showToast({
          title: "请先去[我的->个人信息]界面完成身份验证~",
          icon: 'none'
        })
        searchDisabled = false
      } else if (!(userInfo.if_valid == 'Y')) {
        wx.showToast({
          title: "请等待身份验证通过~",
          icon: 'none'
        })
        searchDisabled = false
      } else {
        wx.navigateTo({
          url: `../campus-edit/campus-edit`,
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', { data: userInfo })
          }
        })
        searchDisabled = false
      }
    }
  },
  goComment(event) {
    console.log("goComment")
    console.log(event)
    wx.navigateTo({
      url: '../../pages/campus-comment/campus-comment?type=0&itemId='+event.currentTarget.dataset.itemId,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        console.log(event.currentTarget.dataset.item)
        res.eventChannel.emit('acceptDataFromOpenerPage', { 
          data: event.currentTarget.dataset.item
        })
      }
    })
  },
  ChangeSidebar(){
    this.setData({"showSidebar":!this.data.showSidebar})
    console.log('taps',this.data.showSidebar)
  }
})